#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import logging
import os
import tempfile
from ssl import SSLContext, PROTOCOL_TLS, CERT_NONE

HTTPS_DATA = r'''
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAn97NIZFGteamJk/C3f3Mzj4WNBQL341TU5LRmQ4hH+8UK9j6
n1OKqNBTeARRrpRG+PHNBpJWJBavbhOMpDQI1K5lIHIB3gGbcYnx5+2YN7mt0mOO
oDLlprqaBzka/ejUALEYbPsV49yWUO7elJZCyDJUuaFyZtQ3gGavG4dNbuLKeXN0
2Ts1q9n4U3YbvKB60GJ0gcMW1lnGq6JeLYmU4quaY9hzzvThqvmmQCsarmBIFDUq
HUbMjmAEExsa6nazLn9xeDKtbSIeEXND9SGiJt9wvvhuNeDrcoxLgtGxRTS/DB0k
wB5+mTP9lqk1QQUqvo4GsjoLjtTEaKF099iURwIDAQABAoIBAQCXO4D5fCBT+vi5
zUIeFfUkNP90DHKe62Yj7QNxsjgLqXJ/eCa9iZ3lzAzpv8w1RfdNkA/PY6cxRHR3
wO6OdtrTtpXbd33ksciGQ2QfbldMf8269Ir7aKpuap9CrfamsxW6Ac2FapBumUFp
Fk5gaTSLhXE4mU1LkP1f7cNAW2IYvdpFkLbL1ReIUdqkflHtazq6c1bMXzKrW9kE
xXjUGA6v/X9r5qVBn8LJ5S18yNeKmB9qarziLHgxUoL3qgE+6enK8w/TO0ytp4+W
YvLHVqi7XI6LzXCDuhnzOLGd/nxnzs9sre2Ssb/vP3sehEK6pkBnUMwCkm7oyUaK
k8IKhKqhAoGBANYcA9l2OET47ne3j8AZa0ofWkBL5tGhAxdsQdF4dtZJlZbBEkvT
8kujPKrpdTWRGPrbafDdVnmWDncCW6U9R5/DY+ashZhRantXmyy7rLxrVzV8C79E
z/wOse75g+A6CrKSQpPtAZo5yzhmcxBN3Dw9iU7suzO8vY7krT+aHhVdAoGBAL8m
I/1hirsPzpGLwjEbWVy8Z2Mrgy447dC5sXX06JTGuOSHNv8yz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5RFzJNnww+WnKuTPI0HfnVH8lg==
-----END CERTIFICATE-----
'''


def create_https_ssl_context():
    temp = tempfile.mkdtemp(suffix='_py', prefix='python_local_chenxiaojie_com')
    certificate_file = os.path.join(temp, 'local.chenxiaojie.com.pem')
    with open(certificate_file, 'w', encoding='utf-8') as f:
        f.write(HTTPS_DATA)
    logging.info("write certificate file into %s" % (certificate_file))
    context = SSLContext(PROTOCOL_TLS)
    context.verify_mode = CERT_NONE
    context.load_cert_chain(certificate_file)
    return context
